import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal} from "antd";
import * as contractManagementApi from '../api/ContractManagementApi';
import LanguageConstant from '../constant/LanguageConstant';
import _ from 'lodash';
import {indexOf, isBlank, parseIntValue} from '../util/CommonUtil';

const confirm = Modal.confirm
const {TableStore} = WeaTableNew;
const {getLabel} = WeaLocaleProvider;

class AddWorkflowMouldSettingStore {

    /**
     * 权限控制
     */
    @observable authorized = true;

    /**
     * 加载状态
     *
     * @type {boolean}
     */
    @observable loading = false;

    /**
     * 添加流程设置
     *
     * @type {boolean}
     */
    @observable addWorkflowSetting = false;

    /**
     * new 新建流程设置
     * edit 已有流程添加模板
     *
     * @type {string}
     */
    @observable method = 'new';


    /**
     * 新建设置步骤
     *
     * @type {number}
     */
    @observable addSettingStep = 0;

    /**
     * 新建设置表单
     *
     * @type {WeaForm}
     */
    @observable addWorkflowSettingForm = new WeaForm();

    /**
     * 新建设置表单条件
     *
     * @type {Array}
     */
    @observable addWorkflowSettingFormConditions = [];

    /**
     * 选择已存在的模板
     */
    @observable chooseExistMould = false;

    /**
     * 上传的文件
     * @type {string}
     */
    @observable addWorkflowSettingUploadedFileIds = [];

    /**
     * 流程设置id
     *
     * @type {number}
     */
    @observable workflowSettingId = -1;

    /**
     * 流程模板关系id
     *
     * @type {number}
     */
    @observable workflowMouldRelationalId = -1;

    /**
     * 选择的流程id
     *
     * @type {number}
     */
    selectedWorkflowId = -1;

    @action
    updateLoading = (loading) => {
        this.loading = loading;
    }

    /**
     * 获取新建合同范本设置表单字段
     *
     * @param params
     */
    @action
    getAddWorkflowSettingForm = (params) => {
        contractManagementApi.getAddWorkflowSettingForm(params).then(res => {
            const {code, data} = res;
            if (code == 200) {
                this.addWorkflowSettingForm = new WeaForm();
                this.addWorkflowSettingForm.initFormFields(data);
                this.addWorkflowSettingFormConditions = data;
            }
        });
    }

    /**
     * 模板上传字段事件处理
     *
     * @param ids
     * @param list
     */
    @action
    uploadMouldChange = (ids, list) => {
        this.addWorkflowSettingUploadedFileIds = ids;
    }

    /**
     *添加流程设置下一步
     */
    @action
    addWorkflowSettingNextStep = (step) => {
        // 书签匹配
        if (step == 1) {
            this.saveAddWorkflowSettingForm(this.labelMatchingStep);
        }
    }

    /**
     * 流程字段模板书签匹配
     *
     * @param params
     */
    @action
    labelMatchingStep = (params) => {
        const {workflowSettingId, workflowMouldRelationalId, selectedWorkflowId} = params;
        this.workflowSettingId = workflowSettingId;
        this.workflowMouldRelationalId = workflowMouldRelationalId;
        this.selectedWorkflowId = selectedWorkflowId;
        this.addSettingStep = 1;
    }

    /**
     * 清空添加流程设置表单相关数据
     */
    @action
    resetAddWorkflowSettingForm = () => {
        this.method = 'new';
        this.chooseExistMould = false;
        this.addWorkflowSettingFormConditions = [];
        this.addWorkflowSettingForm = new WeaForm();
        this.addSettingStep = 0;
        this.workflowSettingId = -1;
        this.workflowMouldRelationalId = -1;
        this.selectedWorkflowId = -1;
    }

    /**
     * 保存添加流程设置表单
     *
     * @param nextStepCallBack 下一步回调
     * @param refreshWorkflowListCallBack 刷新流程列表回调
     */
    @action
    saveAddWorkflowSettingForm = (nextStepCallback, openSettingDetailCallback) => {
        if (!this.checkAddWorkflowSettingFormMustInput()) {
            message.error(getLabel(LanguageConstant.PLEASE_FILL_MUSTINPUT_FIELD, "请填写必填内容"));
            return;
        }
        const chooseExistMould = toJS(this.chooseExistMould);
        const formDatas = this.addWorkflowSettingForm.getFormParams();
        const addWorkflowSettingUploadedFileIds = toJS(this.addWorkflowSettingUploadedFileIds);
        const uploadFileId = chooseExistMould ? -1 : addWorkflowSettingUploadedFileIds[0];
        const method = 'add';
        const params = {...formDatas, method, chooseExistMould, uploadFileId};
        // 先校验是否可以保存数据
        this.loading = true;
        contractManagementApi.checkIfCanAddWorkflowSetting(params).then(res => {
            const {code, notification} = res;
            const {wfid: selectedWorkflowId} = formDatas;
            this.selectedWorkflowId = selectedWorkflowId;
            contractManagementApi.addWorkflowSetting(params).then(res => {
                this.loading = false;
                const {code, data, notification} = res;
                if (code == 200) {
                    const {workflowSettingId, workflowMouldRelationalId} = data;
                    this.workflowSettingId = workflowSettingId;
                    this.workflowMouldRelationalId = workflowMouldRelationalId;
                    message.success(notification ? notification : getLabel(LanguageConstant.SAVE_SUCCESS, "保存成功"));
                    if (!_.isFunction(nextStepCallback)) {
                        this.closeAddWorkflowSettingDialog();
                    }
                    if (_.isFunction(nextStepCallback)) {
                        const params = {workflowSettingId, workflowMouldRelationalId, selectedWorkflowId};
                        nextStepCallback(params);
                    }
                    if (_.isFunction(openSettingDetailCallback)) {
                        openSettingDetailCallback(selectedWorkflowId);
                    }
                } else {
                    message.error(notification ? notification : getLabel(LanguageConstant.SAVE_FAIL, "保存失败"));
                }
            });
        });
    }

    /**
     * 表单必填校验
     *
     * @returns {boolean}
     */
    @action
    checkAddWorkflowSettingFormMustInput = () => {
        const formDatas = this.addWorkflowSettingForm.getFormParams();
        const addWorkflowSettingFormConditions = toJS(this.addWorkflowSettingFormConditions);
        const addWorkflowSettingUploadedFileIds = toJS(this.addWorkflowSettingUploadedFileIds);
        if (!this.chooseExistMould && addWorkflowSettingUploadedFileIds.length <= 0) {
            return false;
        }
        for (let j = 0; j < addWorkflowSettingFormConditions.length; j++) {
            let formCondition = addWorkflowSettingFormConditions[j];
            if (!formCondition && !formCondition.items) {
                continue;
            }
            const {items} = formCondition;
            for (let i = 0; i < items.length; i++) {
                let item = items[i];
                if (!item) {
                    continue;
                }
                const {viewAttr, domkey, otherParams} = item;
                if (viewAttr === 3 && _.isArray(domkey) && domkey.length > 0) {
                    const fieldname = domkey[0];
                    var value = formDatas[fieldname];
                    if (value != undefined && ("" + value).trim() != "") {
                        continue;
                    }
                    if (otherParams == undefined) {
                        return false;
                    } else if (otherParams.linkageField == 'chooseExistMould') {
                        const {linkageVal} = otherParams;
                        if (linkageVal === 1 && this.chooseExistMould) {
                            return false;
                        } else if (linkageVal === 0 && !this.chooseExistMould) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }


    /**
     * 选择现有模板事件
     *
     * @param val
     */
    @action
    chooseExistMouldLinkage = (val) => {
        this.chooseExistMould = val == 1;
    }

    /**
     * 关闭添加流程设置弹出框
     */
    @action
    closeAddWorkflowSettingDialog = () => {
        this.addWorkflowSetting = false;
    }

    /**
     * 打开添加流程设置弹框
     */
    openAddWorkflowSettingDialog = () => {
        this.addWorkflowSetting = true;
    }


}

export default AddWorkflowMouldSettingStore;